//
//  CollectionVideoTableViewCell.m
//  miaopai
//
//  Created by duyifang on 15/11/14.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "CollectionVideoTableViewCell.h"
#import "CustomerAVPlayer.h"
#import "AutoadaptationFrame.h"
#import <SDImageCache.h>
#import <UIImageView+WebCache.h>
@interface CollectionVideoTableViewCell ()

@property(nonatomic, retain) UIImageView *userImageView;

@property(nonatomic, retain) UILabel *titleLabel;

@property(nonatomic, retain) UILabel *timeAndReadNumberLabel;

@property(nonatomic, retain) CustomerAVPlayer *myselfAVPlayer;

@property(nonatomic, retain) UILabel *introduceLabel;

//@property(nonatomic, retain) UIButton *pushToTopicButton;

@property(nonatomic, retain) UIView *separateLine;

@property(nonatomic, retain) NSMutableArray *buttonsArray;

@end

@implementation CollectionVideoTableViewCell

- (void)dealloc
{
    [_userImageView release];
    [_titleLabel release];
    [_timeAndReadNumberLabel release];
    [_myselfAVPlayer release];
    [_introduceLabel release];
    [_separateLine release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self creatSubViews];
    }
    return self;
}

- (void)creatSubViews
{
    self.buttonsArray = [NSMutableArray array];
    for (int i = 0; i < 3; i ++) {
        UIButton *pushToTopicButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [pushToTopicButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        pushToTopicButton.titleLabel.font = [UIFont systemFontOfSize:13];
        pushToTopicButton.layer.cornerRadius = 10;
        pushToTopicButton.layer.masksToBounds = YES;
        pushToTopicButton.layer.borderWidth = 0.5;
        pushToTopicButton.layer.borderColor = [UIColor orangeColor].CGColor;
        [pushToTopicButton addTarget:self action:@selector(toView:) forControlEvents:UIControlEventTouchUpInside];
        [_buttonsArray addObject:pushToTopicButton];
        [self.contentView addSubview:pushToTopicButton];
    }
    
    self.userImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_userImageView];
    [_userImageView release];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel release];
    
    self.timeAndReadNumberLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_timeAndReadNumberLabel];
    [_timeAndReadNumberLabel release];
    
    self.myselfAVPlayer = [CustomerAVPlayer shareCustomerAVPlayer];
    
    self.posterImageView = [[UIImageView alloc] init];
    _posterImageView.userInteractionEnabled = YES;
    [self.contentView addSubview:_posterImageView];
    [_posterImageView release];
    
    self.introduceLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_introduceLabel];
    [_introduceLabel release];
    
    self.separateLine = [[UIView alloc] init];
    [self.contentView addSubview:_separateLine];
    [_separateLine release];
}

- (void)toView:(UIButton *)sender
{
    [self.delegate toTopicView:sender.titleLabel.text];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _userImageView.frame = CGRectMake(10, 5, 35, 35);
    _userImageView.layer.cornerRadius = _userImageView.frame.size.width / 2;
    _userImageView.layer.masksToBounds = YES;
    
    _titleLabel.frame = CGRectMake(_userImageView.frame.origin.x + _userImageView.frame.size.width + 8, _userImageView.frame.origin.y / 2, self.frame.size.width - (_userImageView.frame.origin.x) * 2 - 8 - _userImageView.frame.size.width, 20);
    
    _timeAndReadNumberLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.size.height + _titleLabel.frame.origin.y * 2, _titleLabel.frame.size.width, _titleLabel.frame.size.height);
    
    _posterImageView.frame = CGRectMake(0, _timeAndReadNumberLabel.frame.origin.y + _timeAndReadNumberLabel.frame.size.height + 5, self.frame.size.width, self.frame.size.width * 3 / 4);
    _posterImageView.backgroundColor = [UIColor yellowColor];
    
    _myselfAVPlayer.frame = CGRectMake(0, 0, _posterImageView.frame.size.width, _posterImageView.frame.size.height);

    _introduceLabel.frame = CGRectMake(_userImageView.frame.origin.x, _posterImageView.frame.origin.y + _posterImageView.frame.size.height + 10, self.frame.size.width - 20, [AutoadaptationFrame heightOfLabel:self.channel.ext.t andTextFont:[UIFont systemFontOfSize:15] andLabelWidth:self.frame.size.width - 20]);
    
    for (int i = 0; i < 3; i++) {
        
        UIButton *pushToTopicButton = [self.buttonsArray objectAtIndex:i];
        
        [pushToTopicButton removeFromSuperview];
    }
    
    CGFloat buttonWidth = 0;
    for (int i = 0; i < _channel.topics.count; i++) {
        if (i < 3) {
            UIButton *pushToTopicButton = [self.buttonsArray objectAtIndex:i];
            CGFloat buttonWordsWidth =[AutoadaptationFrame widthOfLabel:self.channel.topics[i] andTextFont:[UIFont systemFontOfSize:13] andLableHeight:20];
            pushToTopicButton.frame = CGRectMake(_introduceLabel.frame.origin.x + buttonWidth, _introduceLabel.frame.origin.y + _introduceLabel.frame.size.height + 15, buttonWordsWidth + 10, 20);
            [pushToTopicButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [pushToTopicButton setTitle:self.channel.topics[i] forState:UIControlStateNormal];
            buttonWidth += pushToTopicButton.frame.size.width + 5;
            [self.contentView addSubview:pushToTopicButton];
        }else
        {
            break;
        }
        
    }
    
    _separateLine.frame = CGRectMake(0, self.frame.size.height - 5, self.frame.size.width, 5);
    _separateLine.backgroundColor = [UIColor colorWithRed:0.7533 green:0.7533 blue:0.7533 alpha:1.0];
}

- (void)setChannel:(Channel *)channel
{
    if (_channel != channel) {
        [_channel release];
        _channel = [channel retain];
    }
    
    [_userImageView sd_setImageWithURL:[NSURL URLWithString:channel.ext.owner.icon] placeholderImage:[UIImage imageNamed:@"iconfont-yonghu"]];
    
    _titleLabel.text = channel.ext.owner.nick;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    
    _timeAndReadNumberLabel.textColor =[UIColor colorWithRed:0.5141 green:0.5141 blue:0.5141 alpha:1.0];
    NSString *numberAll = [NSString stringWithFormat:@"%@  热度", channel.stat.vcntNice];
    NSMutableAttributedString *number = [[NSMutableAttributedString alloc] initWithString:numberAll];
    NSRange range1 = [numberAll rangeOfString:channel.stat.vcntNice];
    [number addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range1];
    [_timeAndReadNumberLabel setAttributedText:number];
    _timeAndReadNumberLabel.font = [UIFont systemFontOfSize:14];
    [number release];
    
    [_posterImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", channel.pic.base, channel.pic.m]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];

    _introduceLabel.numberOfLines = 0;
    _introduceLabel.text = channel.ext.t;
    _introduceLabel.font = [UIFont systemFontOfSize:15];
    _introduceLabel.textColor = [UIColor colorWithRed:0.5141 green:0.5141 blue:0.5141 alpha:1.0];
    
}

- (void)setStream:(NSString *)stream
{
    if (![_stream isEqualToString:stream]) {
        _myselfAVPlayer.frame = CGRectMake(0, 0, _posterImageView.frame.size.width, _posterImageView.frame.size.height);
        _myselfAVPlayer.playURL = stream;
        [self.posterImageView addSubview:_myselfAVPlayer];
    }
    if (_stream != stream) {
        [_stream release];
        _stream = [stream copy];
    }
    
}

- (void)setPlayURL:(NSString *)playURL
{
    if (_playURL != playURL) {
        [_playURL release];
        _playURL = [playURL copy];
    }
    _myselfAVPlayer.frame = CGRectMake(0, 0, _posterImageView.frame.size.width, _posterImageView.frame.size.height);
    _myselfAVPlayer.playURL = playURL;
    [self.posterImageView addSubview:_myselfAVPlayer];
}

- (void)setRemoveVideo:(NSString *)removeVideo
{
    if (_removeVideo != removeVideo) {
        [_removeVideo release];
        _removeVideo = [removeVideo copy];
    }
    [self.myselfAVPlayer removeFromSuperview];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
